﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ComplexNumbers
{
    public class Descard : Complex
    {
        private double real;
        private double imaginary;

        public Descard(double real, double imaginary)
        {
            this.real = real;
            this.imaginary = imaginary;
        }

        override public double Real
        {
            get { return real; }
        }

        override public double Imaginary
        {
            get { return imaginary; }
        }

        public static implicit operator Polar(Descard num)
        {
            double abs = Math.Sqrt(num.Real * num.Real + num.imaginary * num.imaginary);
            double angle;
            if (num.Real == 0)
            {
                angle = Math.PI/2;
            }
            else
            {
                angle = Math.Atan(num.Imaginary / num.Real);
                if (num.Real < 0)
                {
                    angle += Math.PI;
                }
            }
            return new Polar(abs, angle);

        }

        public static Descard operator +(Descard first, Descard second)
        {
            return new Descard(first.Real + second.Real, second.Imaginary + first.Imaginary);
        }

        public static Descard operator -(Descard first, Descard second)
        {
            return new Descard(first.Real - second.Real, first.Real - second.Real);
        }

        public static Descard operator *(Descard first, Descard second)
        {
            return (Descard)((Polar)first * (Polar)second);
        }

        public static Descard operator /(Descard first, Descard second)
        {
            return (Descard)((Polar)first / (Polar)second);
        }

        public new virtual string ToString()
        {
            return String.Format("({0} + {1}i)", real, imaginary);
        }
    }
}
